import { reqGetGoodsInfo, reqAddOrUpdateShopCart } from "@/api"
import { getUUID } from '@/utils/uuid_token'

const state = {
    goodInfo: {},
    uuid_token: getUUID()
}

const mutations = {
    GETGOODINFO(state, goodinfo) {
        state.goodInfo = goodinfo;
    }
}

const actions = {
    async getGoodInfo({ commit }, skuid) {
        const res = await reqGetGoodsInfo(skuid)
        console.log(res)
        if (res.code == 200) {
            commit('GETGOODINFO', res.data)
        }
    },
    async addOrUpdateShopCart({ commit }, { skuid, skuNum }) {
        const res = await reqAddOrUpdateShopCart(skuid, skuNum)
        if (res.code == 200) {
            return "ok"
        } else {
            return Promise.reject(new Error("error"))
        }
    }
}

const getters = {
    categoryView(state) {
        return state.goodInfo.categoryView || {};
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || [];
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}